package br.com.android.financas.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.joda.time.DateTime;

import android.app.Activity;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.TextView;
import br.com.android.financas.fragments.DatePickerFragment;

public class DataAdapter extends ArrayAdapter<String> {

	private Activity activity;
	private DateTime minimo;
	private DateTime dataSelecionada;
	private boolean soDia=false;

	public DataAdapter(Context context, int textViewResourceId,List<String> list) {

		super(context, textViewResourceId, list);

		this.activity=(Activity) context;
		this.minimo=minimo;
	}

	@Override
	public View getDropDownView(int position, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return getCustomView(position, convertView, parent);
	}

	public View getCustomView(int position, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		// return super.getView(position, convertView, parent);

		LayoutInflater inflater = activity.getLayoutInflater();
		View row = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
		final TextView item = (TextView) row.findViewById(android.R.id.text1);
		String valor=getItem(position);

		if(dataSelecionada==null){
			try {
				dataSelecionada=new DateTime(new SimpleDateFormat("dd/MM/yyyy").parse(valor).getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		item.setText(valor);
		item.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(final View v) {

				OnDateSetListener ondate = new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						Date data = new Date(view.getCalendarView().getDate());
						String d=new SimpleDateFormat(!soDia ? "dd/MM/yyyy":"dd").format(data);
						((TextView) v).setText(d);
						dataSelecionada=null;
						clear();
						add(d);
						notifyDataSetChanged();

					}
				};

				DatePickerFragment newFragment = new DatePickerFragment();
				newFragment.setCallBack(ondate);
				newFragment.setMinimo(minimo);
				newFragment.setDataSelecionada(dataSelecionada);
				newFragment.setSoDia(soDia);
				newFragment.show(activity.getFragmentManager(), "datePicker");

			}
		});

		return row;
	}

	public void setSoDia(boolean soDia) {
		this.soDia = soDia;
	}
	public void setMinimo(DateTime minimo) {
		this.minimo = minimo;
	}

	public void setDataSelecionada(DateTime dataSelecionada) {
		this.dataSelecionada = dataSelecionada;
	}

}